package falcon.tool;

import java.io.File;

import falcon.common.Errno;
import falcon.common.FalconConfig;
import falcon.common.FalconUser;
import falcon.common.TextFile;
import falcon.server.FalconServer;
import falcon.server.processor.FalconFlowProcessor;

public final class Falcon {

	public static void main(String[] args) {
		if (args.length < 1) {
			System.out.println("Error: args.length < 1");
			return;
		}
		String path = args[0];
		if (!TextFile.exists(path)) {
			System.out.println(String.format("Error: path not found;path=%s", path));
			return;
		}
		FalconConfig config = new FalconConfig(path);
		int port = config.getPort();
		if (port < 1024) {
			System.out.println(String.format("Error: port < 1024;%s=%d", FalconConfig.Key.PORT, port));
			return;
		}
		String dir = config.getDir();
		if (!TextFile.exists(dir)) {
			System.out.println(String.format("Error: dir not found;%s=%s", FalconConfig.Key.DIR, dir));
			return;
		}
		if (!new File(dir).isDirectory()) {
			System.out.println(String.format("Error: not directory;%s=%s", FalconConfig.Key.DIR, dir));
			return;
		}
		int errno = FalconUser.init(config);
		if (errno != Errno.SUCCESS) {
			System.out.println(String.format("Error: FalconUser.init failure errno=%d", errno));
			return;
		}
		if (!FalconFlowProcessor.init(config)) {
			System.out.println(String.format("Error: FalconFlowProcessor.init failure"));
			return;
		}
		FalconServer server = new FalconServer(config);
		server.run();
	}
}
